/**
 * 
 */
package com.apache.portal.common.connector.impl;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.apache.api.vo.ParamsVo;
import com.apache.portal.common.annotion.AnntationBean;
import com.apache.portal.common.connector.BeforlerInterceptorAdapter;
import com.apache.portal.common.util.PortalPubFactory;
import com.apache.tools.StrUtil;
import com.apache.uct.common.LoginUser;

/**
 * 全局添加orgId，deptId
 */
@AnntationBean(name = "setSearchParam")
public class SetSearchParamAdapterImpl extends BeforlerInterceptorAdapter {

	/* (non-Javadoc)
	 * @see com.iussoft.portal.common.connector.InterceptorAdapter#beforPreHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, com.apache.api.vo.ParamsVo)
	 */
	public boolean beforPreHandle(HttpServletRequest request, HttpServletResponse response, ParamsVo param) {
		Map<String, String> resultMap = param.getParams();
		String modelTypes = String.valueOf(resultMap.get("modelTypes"));
		LoginUser loginUser = PortalPubFactory.getInstance().getLoginUser(request);
		String str[] = modelTypes.split(",");
		if (modelTypes.startsWith("s_")) {
			for (int i = 0; i < str.length; i++) {
				String prefix = str[i].substring(2);
				if (StrUtil.isNull(resultMap.get(prefix + ".wi_orgId"))) {
					resultMap.put(prefix + ".wi_orgId", loginUser.getDataOrgIds() + loginUser.getOrgId());
				} else if (StrUtil.isNull(resultMap.get(prefix + ".w_orgId"))) {
					resultMap.put(prefix + ".w_orgId", loginUser.getOrgId());
				} else if (StrUtil.isNull(resultMap.get(prefix + ".wi_deptId"))) {
					resultMap.put(prefix + ".wi_deptId", loginUser.getDataDeptIds() + loginUser.getDeptId());
				} else if (StrUtil.isNull(resultMap.get(prefix + ".w_deptId"))) {
					resultMap.put(prefix + ".w_deptId", loginUser.getDeptId());
				}

			}
		}
		param.setParams(resultMap);
		return true;
	}

}
